package com.propertyportal.alladapter;

import java.util.List;

import com.propertyportal.manageproperty.MenuSection;
import com.propertyportal.manageproperty.R;
import com.propertyportal.manageproperty.SectionItem;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class SectionListAdapter extends BaseExpandableListAdapter{
	private Context context;
	private LayoutInflater inflater;
	private List<MenuSection>sections;
	
	public SectionListAdapter(Context context,List<MenuSection>sections){
		this.context=context;
		this.inflater=LayoutInflater.from(context);
		this.sections=sections;
	}
	
	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return sections.get(groupPosition).getSectionItems().get(childPosition);
	}
	@Override
	public long getChildId(int groupPostion, int childPosition) {
		// TODO Auto-generated method stub
		return sections.get(groupPostion).getSectionItems().get(childPosition).getId();
	}
	@Override
	public int getChildrenCount(int groupPosition) {
		// TODO Auto-generated method stub
		return sections.get(groupPosition).getSectionItems().size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		// TODO Auto-generated method stub
		return this.sections.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		// TODO Auto-generated method stub
		return this.sections.size();
	}

	@Override
	public long getGroupId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null) {
            convertView = inflater.inflate(R.layout.slidingmenu_sectionview,
                    parent, false);
        }

        TextView textView = (TextView) convertView
                .findViewById(R.id.slidingmenu_section_title);
        textView.setText(((MenuSection) getGroup(groupPosition)).getTitle());

        return convertView;
	}
	@Override
	public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView == null) {
            convertView = inflater.inflate(R.layout.slidingmenu_sectionitem,
                    parent, false);
        }

        SectionItem oSectionItem = this.sections.get(groupPosition).getSectionItems().get(childPosition);
       
        TextView textView = (TextView) convertView
                .findViewById(R.id.slidingmenu_sectionitem_label);
        textView.setText(oSectionItem.getName());

        final ImageView itemIcon = (ImageView) convertView
                .findViewById(R.id.slidingmenu_sectionitem_icon);
        
        itemIcon.setImageDrawable(getDrawableByName(
                oSectionItem.getIcon(), this.context));

        return convertView;
	}
	
	 public static Drawable getDrawableByName( String name, Context context ) {
	        int drawableResource = context.getResources().getIdentifier(
	                        name,
	                        "drawable",
	                        context.getPackageName());
	        if ( drawableResource == 0 ) {
	            throw new RuntimeException("Can't find drawable with name: " + name );
	        }
	        return context.getResources().getDrawable(drawableResource);
	    }

}
